Automatically download option data


In [ ]:
%pylab inline

import datetime as dt
import os

import pandas as pd
from pandas.io.data import Options

In [ ]:
outputDirectory = os.environ.get('OUTPUTDIRECTORY', -1)

if outputDirectory == -1:
    stamp = dt.datetime.now().strftime("%Y%m%d_%H%M%S")
    outputDirectory = 'data/'+stamp
    
    print("Warning: Output directory was not retrievable")

if not os.path.exists(outputDirectory):
    os.makedirs(outputDirectory)

In [ ]:
tickers = pd.read_csv('ticker_list.csv')
tickerList = tickers['ticker'].values

In [ ]:
for i in range(len(tickerList)):

    option = Options(tickerList[i], 'yahoo')
    
    try:
        data = option.get_all_data()
    except:
        print('Warning: Unable to load '+tickerList[i])
        # Include a "retry" in the next cell
    else:
        filename = outputDirectory+'/'+tickerList[i]+'.csv'
        data[['Last', 'Vol']].to_csv(filename)

In [ ]:
# Loop through the failed downloads a few times giving them a chance to work

In [ ]:
os.system('git add '+outputDirectory)
os.system('git commit -m "Uploading '+outputDirectory+'"')
os.system('git push')